﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using System.Data;
namespace AirPlane
{
    public partial class XacNhanNangHanVe : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["islogin"] == null || (bool)Session["islogin"] == false || Session["type"].ToString() != "1")
            {

                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                if (!IsPostBack && Request["id"] != null && Request["typenew"] != null)
                {
                    loadData(Request["id"].ToString(), int.Parse(Request["typenew"].ToString()));
                    Session["xacnhan"] = 0;
                    Session["typeNew"] = Request["typeold"].ToString();
                }
            }
        }

        protected void btnQuayLai_Click(object sender, EventArgs e)
        {
            Session["xacnhan"] = 0;
            Response.Redirect("NangHangVe.aspx?id="+Request["id"].ToString() + "&type=" +   Session["typeNew"].ToString());
        }

        protected void btnXacNhan_Click(object sender, EventArgs e)
        {
            if ((int)Session["xacnhan"] == 0)
            {
                VeMayBayBUS vmb = new VeMayBayBUS();
                DataTable dt = new DataTable();
                int n = 0;
                dt = vmb.tinhPhiNangHang(Request["id"].ToString());
                string maNhanVien = Session["username"].ToString();
              
                string maChuyenBay = "";
                maChuyenBay = dt.Rows[0]["ID_chuyenBay"].ToString();
                int loaiVeCu = int.Parse(Request["typeold"].ToString());
                int loaiVeMoi = int.Parse(Request["typenew"].ToString());
                float phiChuyenDoi = float.Parse(txtPhi.Text.ToString());
                n = vmb.NangHangVe(Request["id"].ToString(), loaiVeCu, loaiVeMoi, maNhanVien, maChuyenBay, phiChuyenDoi);
                Session["xacnhan"] = 1;
                Session["typeNew"] = Request["typenew"].ToString();
                if (n == 4)
                    lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Bạn đã nâng hạng vé thành công</span>";
                else
                    lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Vé chưa được nâng hạng</span>";
            }
            else
                lbThongBao.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Bạn Vừa Nâng Hạng Vé Này!</span>";
                     

        }
        public void loadData(string maVe,int loaiVeMoi)
        {
            VeMayBayBUS vmb = new VeMayBayBUS();
            BangGiaVeBUS bgv = new BangGiaVeBUS();
            DataTable dt = new DataTable();
            float giaVe = 0;
            float soDu= 0;
            dt = vmb.tinhPhiNangHang(maVe);
            if (dt.Rows.Count == 1)
            {
                if (DateTime.Parse(dt.Rows[0]["ngayDi"].ToString()) > DateTime.Now)
                {
                    txtPhi.Text = dt.Rows[0]["phiNangHangTruocNgayKhoiHanh"].ToString();

                }
                else
                    txtPhi.Text = dt.Rows[0]["phiNangHangSauNgayKhoiHanh"].ToString();

                giaVe = bgv.layGiaVe(dt.Rows[0]["ID_chuyenBay"].ToString(), loaiVeMoi);
                if (giaVe > 0)
                    soDu = giaVe - float.Parse(dt.Rows[0]["giaVe"].ToString());

                txtTraThem.Text = (soDu*float.Parse(dt.Rows[0]["phanTramGiaVe"].ToString())* 1.1).ToString();
                //else
                //{

                //}
                txtTongCong.Text = (soDu*float.Parse(dt.Rows[0]["phanTramGiaVe"].ToString())* 1.1 +  float.Parse( dt.Rows[0]["phiNangHangTruocNgayKhoiHanh"].ToString())).ToString();

            }

        }
    }
}